﻿CREATE PROCEDURE [dbo].[DefekturaGoodsInsert]
(
	@GoodsId int,
	@DefekturaCategoryId int
) 
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
SET NOCOUNT ON

IF (@DefekturaCategoryId Is Null) OR (@GoodsId Is Null)
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

IF EXISTS (SELECT TOP 1 GoodsId FROM PR.DefekturaGoods WHERE GoodsId = @GoodsId)
BEGIN
	SELECT @intErrCode = 2, @strMsg = 'Дублируется товар!'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

	SET NOCOUNT OFF;

INSERT INTO PR.DefekturaGoods
( GoodsId, DefekturaCategoryId) 
VALUES ( @GoodsId, @DefekturaCategoryId);
	
SELECT GoodsId, DefekturaCategoryId
FROM PR.DefekturaGoods WHERE (GoodsId = @GoodsId)

RETURN 0
